<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="__STATIC__/web/js/rem.js"></script>
    <link rel="stylesheet" href="__STATIC__/web/css/public.css">
    <link rel="stylesheet" href="__STATIC__/web/css/register.css">

    <script src="__STATIC__/web/js/jquery.js"></script>
    <link rel="stylesheet" href="__STATIC__/web/css/bui.css">
    <script src="__STATIC__/web/js/bui.js"></script>
    <script src="__STATIC__/web/js/zepto.js"></script>
    <title>注册</title>
</head>

<body>
<div class="bui-page">
    <main>
        <div class="header-top">欢迎登陆山科大技术转移公共服务平台</div>
        <div class="user-login">
            <div class="user-name">
                <span>账号</span>
                <input type="text" placeholder="请输入账号" maxlength="11" id="phone">
            </div>
        </div>

        <div class="user-login">
            <div class="user-name">
                <span>验证码</span>
                <input type="text" style="width: 2.5rem;flex: unset;" placeholder="请输入验证码" id="code">
            </div>
            <button class="getcode" id="changetext">获取验证码</button>
        </div>
        <div class="user-login">
            <div class="user-name">
                <span>密码</span>
                <input type="password" placeholder="请输入密码" id="pwd">
            </div>
        </div>
        <div class="user-login">
            <div class="user-name">
                <span>确认密码</span>
                <input type="password" placeholder="请再次输入密码" id="pwdangin">
            </div>
        </div>
        <div class="btn">
            <button class="submit" onclick="submit()">修改</button>
        </div>
    </main>
</div>
<script>


    // 判断是否为手机号
    function isPoneAvailable(pone) {
        var myreg = /^[1][0-9][0-9]{9}$/;
        if (!myreg.test(pone)) {
            bui.alert("手机号有误");
            return false;
        }
        else if (pone == '' || pone.length <= 10) {
            bui.alert("手机号有误");
            return false;
        }
    }
    $('#changetext').click(function () {
        if (isPoneAvailable($('#phone').val()) == false) {
            return
        } else {
            $.ajax({
                url: "{:url('getCode')}",
                data:{phone:$('#phone').val()},
                type:'POST',
                success(res) {
                    console.log(res)
                    var count = 60;
                    $('#changetext').text('倒计时' + count + 's');
                    var timer = null;
                    timer = setInterval(function () {
                        if (count > 0) {
                            count = count - 1;
                            $('#changetext').text('倒计时' + count + 's');
                            $('#changetext').attr('disabled', "true");
                        }
                        else {
                            clearInterval(timer);
                            $('#changetext').text('获取验证码');
                            $('#changetext').removeAttr("disabled");
                        }
                    }, 1000);
                    if (res.code == 0) {
                        bui.alert("发送成功");
                    } else {
                        bui.alert(res.msg);
                    }
                }
            })
        }
    })
    function submit() {
        if ($('#phone').val().length == 0 || $('#code').val().length == 0 || $('#pwd').val().length == 0 || $('#pwdangin').val().length == 0) {

            bui.alert("请填写完整信息");

            return
        }
        else if ($('#pwd').val() !== $('#pwdangin').val()) {

            bui.alert("密码不一致");

            return
        }
        else {

            const password = $("#pwd").val();
            const password_confirm = $("#pwdangin").val();
            $.ajax({
                url: "{:url('doResetPwd')}",
                data: {
                    code: $('#code').val(),
                    phone: $('#phone').val(),
                    password,
                    password_confirm
                },
                type:'POST',
                success(res) {
                    console.log(res)
                    if (res.code == 0) {
                        bui.alert(res.msg);
                    } else {
                        bui.alert(res.msg);
                        window.location.href = "{:url('login')}"
                    }
                }
            })
        }
    }
</script>
</body>

</html>